'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const $ = db.command.aggregate

	let comment = await db.collection("orders")
		.doc(event.order_id)
		.update({
			comment: event.comment_info,
			score: event.comment_score,
			tags: event.tags,
			// status:0
		})

	let res = await db.collection("orders")
		.aggregate()
		.match({
			paramedic_id: event.paramedic_id,
			// status:0
		})
		.group({
			_id: null,
			score_avg: $.avg('$score'),
			score_count: $.sum(1)
		})
		.end()

	const score_avg = res.data[0].score_avg
	const score_count = res.data[0].score_count

	let paramedic_info = await db.collection("uni-id-users")
		.field({
			'paramedic_profile': true
		})
		.where({
			_id: event.paramedic_id
		})
		.get()

	let paramedic_tags = paramedic_info.data[0].paramedic_profile.tags
	console.log("护工表标签:",paramedic_tags)
	let _paramedic_tags = paramedic_tags.concat(event.tags)
	const tags = [...new Set(_paramedic_tags)]

	let count = await db.collection("uni-id-users")
		.doc(event.paramedic_id)
		.update({
			paramedic_profile: {
				score: score_avg,
				score_count: score_count,
				tags: tags
			}
		})

	//返回数据给客户端
	return count
};
